#include "thread_pool.h"

int main(int argc, char *argv[]){                                  
    ARGS_CHECK(argc,2);
    // 创建配置项数组
    config_item_t configs[MAX_CONFIG_ITEMS];
    int count = parse_config_file(argv[1], configs, MAX_CONFIG_ITEMS);
    
    printf("FTP Server Configuration (%d items):\n", count);
    for (int i = 0; i < count; i++) {
        printf("%s = %s\n", configs[i].key, configs[i].value);
    }
    
    // 提取服务器配置到结构体
    server_config_t server_config = extract_server_config(configs, count);    
    thread_pool_t thread_pool;
    int worker_num=atoi(server_config.work_num);
    thread_pool_init(worker_num,&thread_pool);
    make_worker(&thread_pool);
    int sockfd=tcp_init(server_config.ip,server_config.port);
    int epfd=epoll_create(1);
    epoll_add(epfd,sockfd);
    struct epoll_event readyset[1024];
    while(1){
        int ready_num=epoll_wait(epfd,readyset,1024,-1);
        for(int i=0;i<ready_num;i++){
            if(readyset[i].data.fd==sockfd){
                int netfd=accept(sockfd,NULL,NULL);
                printf("1 client connected! netfd=%d\n",netfd);
                pthread_mutex_lock(&thread_pool.mutex);
                en_queue(&thread_pool.task_queue,netfd);
                pthread_cond_broadcast(&thread_pool.cond);
                pthread_mutex_unlock(&thread_pool.mutex);
            }
        }
    }
    return 0;
}

